package cn.ycms.jdbc;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.ycms.utils.StringUtils;



public class ColumnUtil {
	public static Object getValue(int typeCode, String v) {
		if (StringUtils.isNull(v)) {
			v = "0";
		}
		Object value = null;
		if (typeCode == Types.VARCHAR) {
			value = String.valueOf(v);
		} else if (typeCode == Types.INTEGER) {
			value = Integer.parseInt(v);
		} else if (typeCode == Types.BIGINT) {
			value = Long.parseLong(v);
		} else if (typeCode == Types.DOUBLE) {
			value = Double.parseDouble(v);
		} else if (typeCode == Types.SMALLINT) {
			value = Short.parseShort(v);
		} else if (typeCode == Types.BOOLEAN) {
			value = Boolean.parseBoolean(v);
		} else if (typeCode == Types.DATE) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				value = sdf.parseObject(v);
			} catch (Exception e) {
				e.printStackTrace();
				value = new Date();
			}
		} else if (typeCode == Types.TIMESTAMP) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				value = sdf.parseObject(v);
			} catch (Exception e) {
				e.printStackTrace();
				value = new Date();
			}
		}
		return value;
	}
}
